package com.starbaba.stat.service.impl;

import java.util.List;
import java.util.Map;

import com.base.common.database.BaseDao;
import com.base.common.util.StringUtils;
import com.starbaba.core.web.page.PageRequest;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.stat.model.ArpuStatVO;
import com.starbaba.stat.service.StatArpuService;
import com.starbaba.stat.utils.Constant;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class StatArpuServiceImpl implements StatArpuService {
   
	
    /**
     * 查询arpu值统计数据
     */
    @Override
    public PageResponse queryARPUStatistics(Map<String, Object> paramsMap,
            PageRequest pageRequest, boolean limit) throws Exception {

        int start = pageRequest.getIDisplayStart();
        int length = pageRequest.getIDisplayLength();

        BaseDao dao = new BaseDao(Constant.LOAN_STAT_DB_POOL);
        StringBuffer statSql = new StringBuffer();
        statSql.append("select a.date,a.platform,a.prdid,a.channel,a.channel_name, new_user,");
        statSql.append("averagearpu,averagearpu7,averagearpu14,averagearpu30,averagearpu60 from stat_user_arpu a where 1=1 ");
        fillAPRUCondition(paramsMap, statSql);
        statSql.append(" group by a.date,a.platform,a.prdid,a.channel ");
        statSql.append(" order by a."+ pageRequest.getOrderby() + " ");
        statSql.append(pageRequest.getOrder());
        if (limit) {
            statSql.append(" LIMIT " + start + ", " + length);
        }
        
        List<ArpuStatVO> list = dao.list(statSql.toString(), ArpuStatVO.class);
        
        PageResponse response = new PageResponse();
        response.setAaData(list);
        StringBuffer sqlString = new StringBuffer();
        sqlString.append("select a.date from stat_user_arpu a where 1=1 ");
        fillAPRUCondition(paramsMap, sqlString);
        sqlString.append(" group by a.date,a.platform,a.prdid,a.channel order by a.date desc");
        List<ArpuStatVO> list2 = dao.list(sqlString.toString(), ArpuStatVO.class);
        response.setITotalDisplayRecords(list2.size());
        response.setITotalRecords(list2.size());
        response.setSEcho(pageRequest.getSEcho());
        return response;
    }

    /**
     * 填充APRU查询条件
     * 
     * @param paramsMap
     * @param strSql
     */
    private void fillAPRUCondition(Map<String, Object> paramsMap, StringBuffer strSql) {
        if (paramsMap == null) {
            return;
        }
        String channel = (String) paramsMap.get("channel");
        if (channel != null && !StringUtils.isEmpty(channel)) {
            strSql.append(" and a.channel = " + channel);
        }
        String platform = (String) paramsMap.get("platform");
        if (platform != null && !StringUtils.isEmpty(platform)
                && (platform.equals("android") || platform.equals("ios"))) {
            strSql.append(" and a.platform = '" + platform + "'");
        }
        String prdid = (String) paramsMap.get("prdid");
        if (prdid != null && !StringUtils.isEmpty(prdid)) {
            strSql.append(" and a.prdid = " + prdid);
        }
        String startTime = (String) paramsMap.get("startTime");
        String endTime = (String) paramsMap.get("endTime");
        if (startTime != null) {
            strSql.append(" and a.date >= '" + startTime + "'");
        }
        if (endTime != null) {
            strSql.append(" and a.date <= '" + endTime + " 23:59:59'");
        }
    }
}